<template>
  <div class="dist-box w-card">
    <div class="content-box">
      <div class="title-box">
        <span>上游平台</span>
        <el-button style="margin-left:auto;" @click="goodsUpload">商品更新记录</el-button>
        <el-button style="margin-left:10px;" @click="addRecharge">新增上游充值记录</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="channelName" label="平台名称" align="center">
            <template v-slot="scope">
              {{ scope.row.channelName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="chargeTotal" label="充值总额(元)" align="center" min-width="150">
            <template v-slot="scope">
              <el-link type="primary" @click="lookRechargeFlow(scope.row)">{{ numFormat(scope.row.chargeTotal) || '' }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="usedTotal" label="使用总额(元)" align="center" min-width="150">
            <template v-slot="scope">
              {{ numFormat(scope.row.usedTotal) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="leftFee" label="余额(元)" align="center" min-width="150">
            <template v-slot="scope">
              <el-link type="primary" @click="lookyue(scope.row)">{{ numFormat(scope.row.leftFee) || '' }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="balance" label="上游余额(元)" align="center" min-width="150">
            <template v-slot="scope">
              <el-link v-show="!scope.row.balance" style="font-size:14px" type="primary"
                @click="getBalance(scope.row)">获取</el-link>
              <span v-show="scope.row.balance">{{ numFormat(scope.row.balance) || '' }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="channelUrl" label="平台请求地址" align="center">
            <template v-slot="scope">
              {{ scope.row.channelUrl || '' }}
            </template>
          </el-table-column> -->
          <el-table-column prop="operations" label="操作" align="center" width="180">
            <template v-slot="scope">
              <el-link style="font-size:14px" type="primary" @click="handleClick(1,scope.row)">平台商品下载</el-link>
              <el-link style="font-size:14px;margin-left:10px;" type="primary" @click="handleClick(2,scope.row)">查看商品</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <div>
          </div>
          <!-- <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" /> -->
        </div>
      </div>
    </div>
  </div>
  <replace ref="replaceRef" @dialogClose="getTableData" />
  <goods-upload ref="goodsUploadRef" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { warningDeal, getDateTime, getWindowWidthAndHeight, BlobDownLoad, numFormat } from '@/utils/utils'
import api from '../api'
import { useRouter } from 'vue-router'
import replace from '../components/replace.vue'
import goodsUpload from '../components/goodsUpload.vue'
export default {
  components: {
    replace,
    goodsUpload,
  },
  setup() {
    const router = useRouter()

    const replaceRef = ref(null)
    const goodsUploadRef = ref(null)

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      tableData: [],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
      }
      api.channelList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total || 0
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleClick(type, data) {
      switch (type) {
        case 1:
          //下载
          downFile(data.channelCode)
          break
        case 2:
          // 查看商品
          router.push({ path: '/prodApi/goods', query: { channelCode: data.channelCode, channelName: data.channelName } })
          break
      }
    }

    function downFile(channelCode) {
      const obj = {
        channelCode,
        responseType: "blob",
      }
      api.productDown(obj).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    function getBalance(data) {
      const obj = {
        channelCode: data.channelCode,
      }
      api.channelBalance(obj).then(res => {
        if (res.code == '200') {
          data.balance = res.data.balance
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addRecharge() {
      replaceRef.value.dialogShow()
    }

    function lookRechargeFlow(data) {
      router.push({ path: '/prodApi/rechargeFlow', query: { channelCode: data.channelCode, channelName: data.channelName, chargeTotal: data.chargeTotal } })
    }

    function lookyue(data) {
      router.push({ path: '/prodApi/yueDetail', query: { channelCode: data.channelCode, channelName: encodeURIComponent(data.channelName), leftFee: data.leftFee } })
    }

    function goodsUpload() {
      goodsUploadRef.value.dialogShow()
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 220
      getTableData()
    })

    return {
      ...toRefs(state),
      replaceRef,
      goodsUploadRef,
      handleCurrentChange,
      handleSizeChange,
      handleClick,
      numFormat,
      getBalance,
      getTableData,
      addRecharge,
      lookRechargeFlow,
      lookyue,
      goodsUpload,
    }
  }
}
</script>

<style lang="scss" scoped>
.dist-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
  }
}
.title-box {
  display: flex;
  align-items: center;
  > span {
    font-size: 16px;
    font-weight: bold;
  }
}
.table-box {
  margin-top: 10px;
  .table-footer {
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.search-box {
  display: flex;
  align-items: center;
  gap: 20px;
  box-sizing: border-box;
  padding: 5px;
  margin-top: 10px;
  > div {
    display: flex;
    width: 25%;
    align-items: center;
    > span {
      flex-shrink: 0;
      font-size: 14px;
    }
  }
}
.state2 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ff3939;
}
.state1 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #03b615;
}
</style>